using System;
using System.Collections;
using System.Collections.Generic;

using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace QFramework
{
    [CreateAssetMenu(menuName = "@ItemKit/Create Item")]
    public class Item : ScriptableObject, IItem
    {
        [DisplayLabel("名称")]
        public string Name = String.Empty;
        
        [DisplayLabel("关键字")]
        public string Key = String.Empty;
        
        public Sprite Icon;

        [DisplayLabel("可堆叠")]
        public bool Stackable = true;

        [DisplayIf(nameof(Stackable), false, true)]
        [DisplayLabel("     有最大堆叠数量")]
        public bool HasMaxStackableCount = false;
        
        [DisplayIf(new []{nameof(HasMaxStackableCount), nameof(Stackable)}, new []{false, false}, true)]
        [DisplayLabel("         最大堆叠数量")]
        public int MaxStackableCount;

        [DisplayLabel("是武器")]
        public bool IsWeapon = false;

        public string GetKey => Key;
        public string GetName => Name;
        public Sprite GetIcon => Icon;
        public bool GetBoolean(string propertyName)
        {
            if (propertyName.Equals("IsWeapon"))
            {
                return IsWeapon;
            }
            return false;
        }

        public bool IsStackable => Stackable;
        public bool GetHasMaxStackableCount => HasMaxStackableCount;
        public int GetMaxStackableCount => MaxStackableCount;
    }

#if UNITY_EDITOR
    [CustomEditor(typeof(Item))]
    public class ItemEditor : Editor
    {
        private SerializedProperty mIcon;

        private void OnEnable()
        {
            mIcon = serializedObject.FindProperty("Icon");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            serializedObject.DrawProperties(false,0,"Icon");
                        
            GUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("图标");
            mIcon.objectReferenceValue = EditorGUILayout.ObjectField(mIcon.objectReferenceValue, typeof(Sprite), true, GUILayout.Height(48),GUILayout.Width(48));
            GUILayout.EndHorizontal();
            serializedObject.ApplyModifiedPropertiesWithoutUndo();
        }
    }
#endif
}

